package com.yu.waterstation.app.moudles.task.controller;

import com.yu.waterstation.app.moudles.task.mapper.TaskMapper;
import com.yu.waterstation.common.entity.app.Task;
import com.yu.waterstation.common.entity.app.dto.TaskDTO;
import com.yu.waterstation.common.entity.app.form.FinishTaskForm;
import com.yu.waterstation.app.moudles.task.service.TaskServiceImpl;
import com.yu.waterstation.common.entity.app.TaskLog;
import com.yu.waterstation.common.common.annotation.NeedToken;
import com.yu.waterstation.common.common.annotation.VisitorAccess;
import com.yu.waterstation.common.common.enums.TokenType;
import com.yu.waterstation.common.common.util.result.Result;
import com.yu.waterstation.app.common.util.session.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "送水任务")
@RestController
@RequestMapping("/task")
public class TaskController {
    @Resource
    private TaskServiceImpl taskService;
    @Resource
    private TaskMapper taskMapper;

    @ApiOperation("某楼舍的送水任务")
    @VisitorAccess
    @NeedToken(TokenType.ENUM_worker_token)
    @GetMapping("/dorm")
    public Result<List<TaskDTO>> getAddressHasTaskByDorm(@RequestParam(required = false) String dorm) {
        return Result.ok(taskService.getTaskOfDorm(dorm));
    }

    @ApiOperation("获取单个送水任务")
    @VisitorAccess
    @NeedToken(TokenType.ENUM_student_token)
    @GetMapping("/{taskId}")
    public Result<Task> getById(@PathVariable Integer taskId) {
        return Result.ok(taskMapper.selectById(taskId));
    }

    @ApiOperation("发布送水任务")
    @NeedToken(TokenType.ENUM_student_token)
    @PostMapping("")
    public Result<Integer> addTask(HttpServletRequest request) throws Exception {
        return Result.ok(taskService.addTask(SessionUtil.getStudentIdFromRequest(request)));
    }

    @ApiOperation("取消送水任务")
    @NeedToken(TokenType.ENUM_student_token)
    @DeleteMapping("/student")
    public Result<Integer> cancelTask(HttpServletRequest request) throws Exception {
        return Result.ok(taskService.cancelTask(SessionUtil.getStudentIdFromRequest(request)));
    }

    @ApiOperation("完成送水任务")
    @NeedToken(TokenType.ENUM_worker_token)
    @PostMapping("/finish")
    public Result<TaskLog> finishTask(@RequestBody FinishTaskForm finishTaskVo, HttpServletRequest request) throws Exception {
        return taskService.finishTask(SessionUtil.getWorkerIdFromRequest(request), finishTaskVo);
    }
}
